package com.example.a10928.weifriend.ui.login

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import com.example.a10928.weifriend.App
import com.example.a10928.weifriend.R
import com.example.a10928.weifriend.http.api.VariableName
import com.example.a10928.weifriend.ui.BaseActivity
import com.example.a10928.weifriend.util.L
import com.example.a10928.weifriend.util.RxUtil
import io.reactivex.functions.Consumer
import kotlinx.android.synthetic.main.activity_set_pw.*

class SetPWActivity : BaseActivity() {


    var agree:Int=0
    var clickable:Boolean=false

    var visible:Int=0
    var visible_two:Int=0

    override fun getContentView(): Int {
        return R.layout.activity_set_pw
    }

    override fun initView() {

        supportActionBar?.hide()
        iv_pw_visible.setOnClickListener {
            if(visible==0){
                iv_pw_visible.setBackgroundResource(R.drawable.pw_visible)
                visible=1
                et_pw.inputType=145
            }else{
                iv_pw_visible.setBackgroundResource(R.drawable.pw_invisible)
                visible=0
                et_pw.inputType=129
            }
        }

        iv_pw_visible_two.setOnClickListener {
            if(visible_two==0){
                iv_pw_visible_two.setBackgroundResource(R.drawable.pw_visible)
                visible_two=1
                et_pw_two.inputType=145
            }else{
                iv_pw_visible_two.setBackgroundResource(R.drawable.pw_invisible)
                visible_two=0
                et_pw_two.inputType=129
            }
        }

        iv_agree.setOnClickListener {
            if(agree==0){
                iv_agree.setBackgroundResource(R.drawable.agree_select)
                agree=1
                change()

            }else{
                iv_agree.setBackgroundResource(R.drawable.agree_unselect)
                agree=0
            }
        }

        et_pw.addTextChangedListener(object : TextWatcher {
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
            override fun afterTextChanged(s: Editable?) {

                change()
            }

        })

        et_pw_two.addTextChangedListener(object :TextWatcher{
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
            override fun afterTextChanged(s: Editable?) {

                change()
            }
        })

        et_code.addTextChangedListener(object :TextWatcher{
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
            override fun afterTextChanged(s: Editable?) {

                change()
            }
        })

        tv_register.setOnClickListener {
            if(clickable){

                App.app().bpService.saveUserMsg(intent.getStringExtra(VariableName.PHONE),
                        intent.getStringExtra(VariableName.CODE),et_pw.text.toString(),
                        et_pw_two.text.toString(),et_code.text.toString())
                        .compose(RxUtil.io2mainLoad(this))
                        .subscribe(
                                Consumer {

                                    App.app().user.saveUser(it.data.user_phone
                                            ,et_pw.text.toString()
                                            ,it.data.user_id.toString()
                                    ," "
                                    ,it.data.user_phone
                                    ,it.data.token
                                    ," "
                                    ,it.data.user_headimg)


                                    var intent= Intent(this,LoginActivity::class.java)
                                    startActivity(intent)
                                    finish()
                                    L.t("注册成功")

                                },
                                Consumer {
                                    RxUtil.errorException(it)
                                }
                        )
            }
        }

    }

    override fun initData() {

    }

    fun change(){
        if(!TextUtils.isEmpty(et_pw.text.toString())&&!TextUtils.isEmpty(et_pw_two.text.toString())&&
                !TextUtils.isEmpty(et_code.text.toString())&&agree==1){
            tv_register.setBackgroundResource(R.drawable.input)
            clickable=true
        }else{
            tv_register.setBackgroundResource(R.drawable.uninput)
            clickable=false

        }
    }

}
